//
//  RanTestViewController.m
//  ThusMyStyle
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "RanTestViewController.h"
#import <TYAttributedLabel/TYAttributedLabel.h>
#import "RanTool.h"
#import "UIView+Ran.h"
#import "RanDynamicTableViewCell.h"
#import "RanDynamicModel.h"
#import "RanBlingLabel.h"

@interface RanTestViewController ()<TYAttributedLabelDelegate, UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, weak)UITableView *tableview;
@property(nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation RanTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    self.dataSource = [NSMutableArray array];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setupUI];
}

- (void)tap
{
    NSLog(@"Tap:  %@", [NSDate date]);
}

- (void)setupUI {
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 88, 100, 50)];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:nil forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateHighlighted];
    return;
    
    TYTextContainer *container = [self container];
    TYAttributedLabel *label = [[TYAttributedLabel alloc] initWithFrame:CGRectMake(10, 77, 300, container.textHeight)];
    [self.view addSubview:label];
    label.textContainer = container;
    label.backgroundColor = [UIColor whiteColor];
    label.delegate = self;
    
    return;
    UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:tableview];
    self.tableview = tableview;
    tableview.dataSource = self;
    tableview.delegate = self;
    [tableview registerClass:[RanDynamicTableViewCell class] forCellReuseIdentifier:@"cell"];

    
    for (int i = 0; i < 20; i++) {
        RanDynamicModel *model = [RanDynamicModel new];
        model.nameContainer = [self container];
        model.locationContainer = [self container];
        model.contentContainer = [self container];
        NSMutableArray *arr = [NSMutableArray array];
        for (int j = 0; j < 10; j++) {
            [arr addObject:[self container]];
        }
        model.comms = arr;
        [self.dataSource addObject:model];
    }

    [self.tableview reloadData];
    
}

- (TYTextContainer *)container
{
    TYTextContainer *container = [[TYTextContainer alloc] init];
    UIFont *font = [UIFont systemFontOfSize:20];
    [container appendLinkWithText:@"Jack: " linkFont: font linkColor:[UIColor blueColor] underLineStyle:kCTUnderlineStyleNone linkData:@"您点击了杰克"];
    [container appendLinkWithText:@"天空飘来五个字,这都不是事,试试就试试\n" linkFont: font linkColor:[UIColor blackColor] underLineStyle:kCTUnderlineStyleNone linkData:@"space"];
    [container addLinkWithLinkData:@"hehe" range:NSMakeRange(0, 20)];
    
    [container appendLinkWithText:@"王晓明" linkFont: font linkColor:[UIColor blueColor] underLineStyle:kCTUnderlineStyleNone linkData:@"您点击了王晓明"];
    [container appendTextAttributedString:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:font color:[UIColor blackColor] string:@"回复"];
    }]];
    [container appendLinkWithText:@"Jack: " linkFont: font linkColor:[UIColor blueColor] underLineStyle:kCTUnderlineStyleNone linkData:@"您点击了杰克"];
    [container appendTextAttributedString:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:font color:[UIColor blackColor] string:@"我感觉你说的不对, 有的时候就疯狂的萨拉交罚款了打算将罚款的撒飞机快打死了附近的凯萨琳减肥的卡死了就疯狂的阿司法局卡桑德拉附近的开始啦;放假打算考虑放假打算看了分大家萨克防雷接地萨克雷锋大家萨克费老大劲萨克雷锋打算借款分类的骄傲是开放了打算减肥快乐的撒娇罚款了大师傅的撒积分卡http:www.baidu.com拉斯的减肥扣篮大赛;范德萨发解答时空裂缝阿斯顿交罚款了的撒;就疯狂的撒风景扣篮大赛;飞阿斯顿333\n"];
    }]];
    

    
    container = [container createTextContainerWithTextWidth:300];
    return container;
}

- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point
{
    if ([textStorage isKindOfClass:[TYLinkTextStorage class]]) {
        
        id linkStr = ((TYLinkTextStorage*)textStorage).linkData;
        NSLog(@"单击了%@", linkStr);
    }
}

- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageLongPressed:(id<TYTextStorageProtocol>)textStorage onState:(UIGestureRecognizerState)state atPoint:(CGPoint)point
{
    if ([textStorage isKindOfClass:[TYLinkTextStorage class]] && state == UIGestureRecognizerStateBegan) {
        
        id linkStr = ((TYLinkTextStorage*)textStorage).linkData;
        NSLog(@"长按了%@", linkStr);

    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RanDynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    RanDynamicModel *model = self.dataSource[indexPath.row];
    cell.nameLabel.textContainer = model.nameContainer;
    cell.locationLabel.textContainer = model.nameContainer;
    cell.contentLabel.textContainer = model.contentContainer;
    cell.comms = model.comms;
    return cell;
}

#pragma mark - UITabelViewDelegagte

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}


@end
